<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        a {
            text-decoration: none;
            color: #08a3f1;
        }

        td {
            line-height: 35px;
        }

        input {
            border: none;
            outline: none;
            text-align: center;
        }

        .inner {
            display: none;
            position: absolute;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
            background-color: rgba(0, 0, 0, 0.3);
        }

        .box {
            position: absolute;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
            width: 900px;
            height: 500px;
            margin: auto;
            background-color: white;
            padding: 10px;
            box-sizing: border-box;
        }

        .box table,
        table input {
            width: 100%;
            background-color: wheat;
        }

        .btns {
            display: flex;
            width: 80%;
            padding: 20px;
            justify-content: center;
            margin: 0 auto;
        }

        .btns button {
            width: 100px;
            height: 35px;
            margin: 20px;
        }
        .btnn{
            width: 100%;
            text-align: center;
        }
        #add{
            margin:20px;
            width: 100px;
            height: 35px;
        }
    </style>
</head>

<body>
    <table onload="getList()" id="table" border="1"
        style="width:100%;text-align: center;border-collapse: collapse;margin-top: 10px;">
    </table>
    <div class="btnn">
        <button id="add" onclick="add()">添加</button>
    </div>
    
    <div class="inner">
        <div class="box">
            <table border="1" style="text-align: center;border-collapse: collapse;margin-top: 10px;">
                <tr>
                    <td><input id="inp2" type="text" placeholder="合同名称" value=""></td>
                    <td><input id="inp3" type="text" placeholder="合同阶段" value=""></td>
                    <td><input id="inp4" type="date" placeholder="开工时间" value=""></td>
                    <td><input id="inp5" type="text" placeholder="合同金额" value=""></td>
                    <td><input id="inp6" type="text" placeholder="合同编号" value=""></td>
                </tr>
            </table>
            <div class="btns">
                <button onclick="btn1()">添加</button>
                <button onclick="clos()">关闭</button>
            </div>

        </div>
    </div>
</body>
<script>
    function getList() {
        let xhr = new XMLHttpRequest();
        xhr.open('GET', 'http://localhost:3000/emp/searchs', true);
        xhr.send();
        xhr.onload = function () {
            let str = JSON.parse(xhr.responseText);
            let tr_td = `<tr>
                       <td>序号</td>
                       <td>合同名称</td>
                       <td>合同阶段</td>
                       <td>开工时间</td>
                       <td>合同金额</td>
                       <td>合同编号</td>
                        <td>操作</td>     
                     </tr>`;
            for (var i = 0; i < str.data.length; i++) {
                let { eid, ename, sex, birthday, salary, deptid } = str.data[i];
                if (sex == 0) {
                    sex = '前期';
                } else if (sex == 1) {
                    sex = '后期';
                }
                let time = new Date(birthday);
                birthday = time.getFullYear() + '-' + ("0" + (time.getMonth() + 1)).slice(-2) + '-' + ("0" + time.getDate()).slice(-2)
                tr_td += `<tr>
                        <td>${i+1}</td>
                        <td>${ename}</td>
                        <td>${sex}</td>
                        <td>${birthday}</td>
                        <td>${salary}</td>
                        <td>${deptid}</td>
                        <td>
                            <a href="javascript:upd(${i+1},'${eid}','${ename}','${sex}','${birthday}','${salary}','${deptid}')">修改</a>
                        </td>
                    </tr> `
            }
            table.innerHTML = tr_td
        }
    }
    getList()
</script>
<script>
    let inner = document.querySelector('.inner')
    function btn1() {
        let xhr = new XMLHttpRequest();
        xhr.open('POST', `http://localhost:3000/emp/add`, true);
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        let sex = inp3.value;
        if (sex == '前期') {
            sex = 1
        } else if (sex == '后期') {
            sex = 0
        }
        xhr.send(`ename=${inp2.value}&sex=${sex}&birthday=${inp4.value}&salary=${inp5.value}&deptid=${inp6.value}`);
        xhr.onload = function () {
            let str = JSON.parse(xhr.responseText);
            console.log(str);
            if (str.code === 200) {
                alert('添加成功');
                inner.style = 'display: none';
                getList()
            } else {
                alert('添加失败');
                getList()
            }
        }
    }

    function add() {
        inner.style = 'display: block';
    }
    function clos() {
        inner.style = 'display: none';
    }
</script>

</html>